package com.yun.dz.dao;

import org.apache.commons.dbcp2.BasicDataSource;

import javax.sql.DataSource;

public class DBCPDataSource {

    private volatile static DBCPDataSource dbcpDataSource;
    public static DBCPDataSource getSingleton() {
        if (dbcpDataSource == null) {
            synchronized (DBCPDataSource.class) {
                if (dbcpDataSource == null) {
                    dbcpDataSource = new DBCPDataSource();
                }
            }
        }
        return dbcpDataSource;
    }
    private static DataSource dataSource = null;


    private DBCPDataSource() {
    }

    public static DataSource getDataSource(){

        if(dataSource == null){
            // 初始化dataSource变量的值
            BasicDataSource dbcpDataSource = new BasicDataSource();
            dbcpDataSource.setUrl("jdbc:mysql://localhost:3306/jsp_first?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false");
            dbcpDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
            dbcpDataSource.setUsername("root");
            dbcpDataSource.setPassword("lL123456789");

            dbcpDataSource.setDefaultAutoCommit(true);
            //dbcpDataSource.setMaxActive(100);
            dbcpDataSource.setMaxIdle(30);
            //dbcpDataSource.setMaxWait(500);
            dataSource = (DataSource)dbcpDataSource;

        }
        return dataSource;
    }
}